package com.zhouwr.common.metadata;

import com.alibaba.fastjson.JSON;
import com.zhouwr.common.enums.ModelDataType;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.util.StringJoiner;

/**
 * @program: forallcn-iotps
 * @description: 整数数据结构
 * @author: zhouwr
 * @create: 2020-11-19 16:00
 * @version：1.0
 **/
@EqualsAndHashCode(callSuper = true)
@Data
@AllArgsConstructor
@NoArgsConstructor
public class IntData extends Metadata<Integer> {
    private Integer maxValue;
    private Integer minValue;
    private String unit;
    private Integer defaultValue = 0;
    private ModelDataType type = ModelDataType.INT;


    @Override
    public String getUnitName() {
        if (StringUtils.isBlank(unit)) {
            return "";
        }
        return unit.substring(0, unit.indexOf("["));
    }

    @Override
    public String getUnitSymbol() {
        if (StringUtils.isBlank(unit)) {
            return "";
        }
        return unit.substring(unit.indexOf("[") + 1, unit.indexOf("]"));
    }

    @Override
    public Integer getDefaultValue() {
        return this.defaultValue;
    }

    @Override
    public String toString() {
        return new StringJoiner(", ", IntData.class.getSimpleName() + "[", "]")
                .add("maxValue=" + maxValue)
                .add("minValue=" + minValue)
                .add("unit='" + unit + "'")
                .toString();
    }


    public static Metadata<?> form(String metadata) {
        return JSON.toJavaObject(JSON.parseObject(metadata), IntData.class);
    }
}
